package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys_store.Act_Store;
import com.huobao.yunying.adapters.Adapter_StoreList;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.StoreList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class Act_Gonghuoshang extends HuobaoBaseActivity implements OnItemClickListener,OnScrollListener{

	@ViewInject(R.id.listview)
	private ListView listview;
	private Adapter_StoreList adapter;
	private int m_CurrentPage = 1;
	private View footView;
	private TextView footMsg;
	private boolean isLoading = false;

	@Override
	protected int getContentView() {
		return R.layout.layout_listview;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "店铺";
	}

	@Override
	protected void init() {
		listview.setOnItemClickListener(this);
		footView = LayoutInflater.from(context).inflate(R.layout.item_footview, null);
		footMsg = (TextView) footView.findViewById(R.id.tv_message);
		listview.setDivider(getResources().getDrawable(R.mipmap.line));
		listview.setDividerHeight(1);
		listview.addFooterView(footView);
		adapter = new Adapter_StoreList(context,true);
		listview.setAdapter(adapter);
		listview.setOnScrollListener(this);
		getData(true);
	}

	private void getData(boolean showDialog) {
		isLoading = true;
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "member", HttpConst.SECOND_KEY, "supplier",
				 "page", m_CurrentPage+"");
		Http.getInstant().sendRequest(context,showDialog, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<StoreList> list = JSON.parseArray(jsonObject.getString("retval"),StoreList.class);
				if(list == null || list.size() == 0){
					footMsg.setText("加载完成");
					isLoading = true;
					return;
				}
				adapter.addAll(list);
				isLoading = false;
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if((firstVisibleItem + visibleItemCount) >= totalItemCount){
			if(isLoading){
				return;
			}
			isLoading = true;
			m_CurrentPage ++;
			getData(false);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		if(position == adapter.getCount())return;
		String store_id = adapter.getItem(position).getStore_id();
		Intent intent = new Intent(this, Act_Store.class);
		intent.putExtra("id", store_id);
		startActivity(intent);
		
	}

}
